﻿#ifndef SYNCMANAGER_H
#define SYNCMANAGER_H

#include <mutex>
#include <condition_variable>
#include <map>
#include <stdint.h>
#include <atomic>

typedef uint64_t SyncPoint;

class SyncManager
{
public:
    // 删除拷贝构造函数和拷贝赋值操作符
    SyncManager(SyncManager const&) = delete;
    SyncManager& operator=(SyncManager const&) = delete;
    // 提供一个全局唯一的接口
    static SyncManager& getInstance() {
        static SyncManager instance;
        return instance;
    }
    // generate a new syncPoint
    SyncPoint generateUniqueSyncPoint();
    // wait for the release
    uint64_t waitForSyncPoint(SyncPoint syncPointId);   
    // release the target syncPoint
    void releaseSyncPoint(SyncPoint syncPointId, uint64_t id);
    // close syncManager
    void stop();

private:
    // 将构造函数声明为私有
    SyncManager() = default;

    std::atomic<uint64_t> next_id{1};
    std::map<uint64_t, std::shared_ptr<std::condition_variable>> id_map;
    std::map<uint64_t, uint64_t> response_map;
    std::mutex id_mutex;

};

#endif // SYNCMANAGER_H
